package com.ysd.gulimall.coupon.controller;

import java.util.*;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ysd.gulimall.coupon.entity.SmsSeckillSkuRelationEntity;
import com.ysd.gulimall.coupon.feign.ProductFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ysd.gulimall.coupon.entity.SmsSeckillSessionEntity;
import com.ysd.gulimall.coupon.service.SmsSeckillSessionService;
import com.ysd.common.utils.PageUtils;
import com.ysd.common.utils.R;



/**
 * 秒杀活动场次
 *
 * @author zhangjianzhong
 * @email ziziss@163.com
 * @date 2020-07-14 18:01:35
 */
@RestController
@RequestMapping("coupon/seckillsession")
public class SmsSeckillSessionController {
    @Autowired
    private SmsSeckillSessionService smsSeckillSessionService;
    @Autowired
    ProductFeignService pfs;

    @ResponseBody
    @GetMapping("/index")
    public  List<Map<String,Object>> getIndex() {
        List<Map<String,Object>> lsp=new ArrayList<Map<String,Object>>();
        List<SmsSeckillSessionEntity> seckillSessionEntities = smsSeckillSessionService.getLates3DaySession();
        if(seckillSessionEntities==null){
            return  lsp;
        }
        List<SmsSeckillSkuRelationEntity> ls= seckillSessionEntities.get(0).getRelationSkus();
        for(SmsSeckillSkuRelationEntity s:ls){
            Long skuId= s.getSkuId();
            try {
                R r=  pfs.getSkuInfo(skuId);
                if(r.getCode()==0){
                    Map  ms=(Map)JSONObject.toJSON(r.get("skuInfo"));
                    ms.put("newPrice",s.getSeckillPrice());
                    lsp.add(ms);
                }
            }catch (Exception e){

            }
        }
        return lsp;
    }
    /**
     * 查询最近三天需要参加秒杀商品的信息
     * @return
     */
    @GetMapping(value = "/Lates3DaySession")
    public R getLates3DaySession() {

        List<SmsSeckillSessionEntity> seckillSessionEntities = smsSeckillSessionService.getLates3DaySession();

        return R.ok().setData(seckillSessionEntities);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("coupon:smsseckillsession:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = smsSeckillSessionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("coupon:smsseckillsession:info")
    public R info(@PathVariable("id") Long id){
		SmsSeckillSessionEntity smsSeckillSession = smsSeckillSessionService.getById(id);

        return R.ok().put("smsSeckillSession", smsSeckillSession);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("coupon:smsseckillsession:save")
    public R save(@RequestBody SmsSeckillSessionEntity smsSeckillSession){
		smsSeckillSessionService.save(smsSeckillSession);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("coupon:smsseckillsession:update")
    public R update(@RequestBody SmsSeckillSessionEntity smsSeckillSession){
		smsSeckillSessionService.updateById(smsSeckillSession);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("coupon:smsseckillsession:delete")
    public R delete(@RequestBody Long[] ids){
		smsSeckillSessionService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
